#include "wt.h"

int nextcountervalue()
{
	static int x=0;
	return (x++)&0xFF;
}

#define WStyle_Customize 0
#define WStyle_Splash (0x0000000e | 0x00000001)

static QLabel* smallFontLabel(const char *str, int size=8)
{
	static QFont font;
	font.setPointSize(size);
	QLabel* l = new QLabel(str);
	l->setFont(font);
	return l;
}

WT::WT(QWidget *parent) 
  : QWidget(parent, (Qt::WindowFlags)(WStyle_Customize | WStyle_Splash) ) // no title bar
{
	this->resize(320, 240);

	/* title */
	QHBoxLayout *titleLayout = new QHBoxLayout;
	titleLayout->addWidget( smallFontLabel("Simulator for Vernier Temperature Probe", 9), 0, Qt::AlignLeft );
	titleLayout->addStretch();
	titleLayout->addWidget( smallFontLabel("homer.xing@gmail.com"), 0, Qt::AlignRight );
	
	/* table */
	QGridLayout *gridLayout = new QGridLayout;
	gridLayout->addWidget( new QLabel("status:"), 0, 0, Qt::AlignTop );
	gridLayout->addWidget( this->Lstate=smallFontLabel("", 15), 0, 1 );
	gridLayout->addWidget( new QLabel("period (ms):"), 1, 0, Qt::AlignTop );
	gridLayout->addWidget( this->Lperiod=smallFontLabel("", 15), 1, 1 );
	gridLayout->addWidget( new QLabel("LED:"), 2, 0, Qt::AlignTop );
	gridLayout->addWidget( this->Lledcolor=smallFontLabel("", 15), 2, 1 );
	gridLayout->addWidget( new QLabel("temperature:"), 3, 0, Qt::AlignTop );
	gridLayout->addWidget( this->Ltemperature=smallFontLabel("", 22), 3, 1 );
	gridLayout->addWidget( new QLabel("memory:"), 4, 0, Qt::AlignTop );
	gridLayout->addWidget( this->Lmemory=new QLabel(""), 4, 1 );
	QHBoxLayout *dummy = new QHBoxLayout;
	dummy->addLayout(gridLayout);
	dummy->addStretch();

	/* alloc urbmsgs buffer and urb labels*/
	int k;
	for(k=0;k<4;k++)
	  {
		this->urbmsgs[k] = new char[100];
		strcpy(this->urbmsgs[k], "<none>");
		this->urb[k] = smallFontLabel(this->urbmsgs[k], 9);
	  }
	
	QGridLayout *urbLayout = new QGridLayout;
	urbLayout->addWidget( new QLabel("urb: "), 0, 0, Qt::AlignHCenter |  Qt::AlignTop );
	urbLayout->addWidget( this->urb[0], 0, 1, Qt::AlignLeft );
	urbLayout->addWidget( this->urb[1], 1, 1, Qt::AlignLeft );
	urbLayout->addWidget( this->urb[2], 2, 1, Qt::AlignLeft );
	urbLayout->addWidget( this->urb[3], 3, 1, Qt::AlignLeft );
	
	QHBoxLayout *dummy2 = new QHBoxLayout;
	dummy2->addLayout(urbLayout);
	dummy2->addStretch();
	
	/* put them together */
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(titleLayout);
	mainLayout->addLayout(dummy);
	mainLayout->addLayout(dummy2);
	mainLayout->addStretch();
	
	this->setLayout(mainLayout);
	
	this->initialize();
}

void WT::initialize()
{
	this->setstate(STOP);
	this->setmemory( (const unsigned char *)"\0\0\0\0\0\0", 6 );
	this->setperiod(100);
	this->setledcolor(OFF);
	this->temperature = 26;
	this->showtemperature();
}

void WT::setstate(int state)
{
	const char *text[] = {"STOP", "START"};
	if(state>=0 && state<2)
	  {
		this->state = state;
		this->Lstate->setText(text[state]);
	  }
}

int WT::getstate() { return this->state; }

void WT::setmemory(const unsigned char *memory, int len)
{
	char buffer[30];
	if(memory==0 || len<=0 || len>6) return;
	memcpy(this->memory, memory, len);
	sprintf(buffer, "%02x %02x %02x %02x %02x %02x", 
	  this->memory[0], this->memory[1], this->memory[2], this->memory[3], this->memory[4], this->memory[5]);
	this->Lmemory->setText(buffer);
}

void WT::getmemory(unsigned char *buffer) { memcpy(buffer, this->memory, 6); }

void WT::setperiod(int period)
{
	if(period>0)
	  {
		this->period=period;
		this->Lperiod->setText( QString("%1").arg(period) );
	  }
}

int WT::getperiod() { return this->period; }

void WT::setledcolor(int ledcolor)
{
	static const char *text[] = {"GREEN", "YELLOW", "OFF"};
	static QColor color[] = {Qt::green, Qt::yellow, Qt::black};
	if(ledcolor>=0 && ledcolor<3)
	  {
		this->ledcolor = ledcolor;
		QPalette palette = this->Lledcolor->palette();
		palette.setColor(QPalette::Foreground, color[ledcolor]);
		this->Lledcolor->setPalette(palette);
		this->Lledcolor->setText(text[ledcolor]);
	  }
}

int WT::getledcolor() { return this->ledcolor; }

void WT::getserial(char *buffer) { if(buffer) memcpy(buffer, "VernTP", 6); }

void WT::showtemperature()
{
	char buffer[20];
	snprintf(buffer, sizeof buffer - 1, "%.2f", this->temperature);
	this->Ltemperature->setText(buffer);
}

void WT::showurb(char *data)
{
	static int id=0;
	
	char *tmp;
	
	if(data==0) return;
	
	tmp = this->urbmsgs[0]; 
	this->urbmsgs[0] = this->urbmsgs[1]; 
	this->urbmsgs[1] = this->urbmsgs[2]; 
	this->urbmsgs[2] = this->urbmsgs[3];
	this->urbmsgs[3] = tmp;

	sprintf(this->urbmsgs[3], "%d: cmd %02x param %02x %02x %02x %02x %02x %02x %02x", 
		++id, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
	
	int j;
	for(j=0;j<4;j++)
		this->urb[j]->setText(this->urbmsgs[j]);
}
